package com.hapzxb.thread.threadlocal;

import java.util.concurrent.TimeUnit;

/**
 * @author hhh
 * @date 2021-05-09 12:00
 */
public class Profiler {
    // 第一个get()  方法调用时，会进行初始化（如果set()方法没有调用），每个线程会调用一次
    private static final ThreadLocal<Long> TIME_THREADLOCAL = new ThreadLocal<Long>(){
      protected Long inintialValue(){
          return System.currentTimeMillis();
      }
    };

    public static final void begin(){
        TIME_THREADLOCAL.set(System.currentTimeMillis());
    }

    public static final Long end(){
        return System.currentTimeMillis()- TIME_THREADLOCAL.get();
    }

    public static void main(String[] args) throws InterruptedException {
        Profiler.begin();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("Cost: " + Profiler.end() + " mills");
    }
}
